<?php
class ich_data{
    public function check($dbRes){
        $_forms = new ich_forms();

        if($dbRes and $this->checkDBTables()){ // db exists and tables exists
            if($this->checkInstllationFiles()){ //any file of installation exists
                exit($_forms->getSummaryForm());
            }
            return 1; //all is ok
        }
        else{ //db dont exists
            if($this->checkCompletInstllationFiles()){ //runn installation
                header('Location:install.php');
                exit();
            }
            else{ // write info about config files
                exit($_forms->getLogin_ConfigErrorForm());
            }
        }
    }

    private function checkInstllationFiles(){
        if(
            file_exists('install.php') or
            file_exists('install/i_data.php') or
            file_exists('install/i_forms.php')
        )
        return true;
        else return false;
    }

    private function checkCompletInstllationFiles(){
        if(
            file_exists('install.php') and
            file_exists('install/i_data.php') and
            file_exists('install/i_forms.php')
        )
        return true;
        else return false;
    }
    
    private function checkDBTables(){
        if(!$this->table_exists('admins')) return false;
        if(!$this->table_exists('sub_category')) return false;
        if(!$this->table_exists('top_category')) return false;
        if(!$this->table_exists('products')) return false;
        if(!$this->table_exists('order_heads')) return false;
        if(!$this->table_exists('order_products')) return false;
        if(!$this->table_exists('chat')) return false;
        if(!$this->table_exists('delivery_payment')) return false;
        if(!$this->table_exists('delivery_types')) return false;
        
        return true;
    }

    public function table_exists ($table) {
	$tables = mysql_list_tables (_DB_NAME_);
	while (list ($temp) = mysql_fetch_array ($tables)) {
		if ($temp == $table) {
			return true;
		}
	}
	return false;
    }

    public function admin_check($login, $psswd){
        $sql = 'select count(*) from admins where A_Nick="'.$login.'" and A_Passwd="'.sha1($psswd).'"';
        $result = mysql_fetch_row(mysql_query($sql));

        return $result[0];
    }
}
?>
